//  QuDanHomeDetailController.m
//  QuDanOEM
//  Created by xiaohuang on 2021/11/7.
#import "QuDanHomeDetailController.h"
#import "QuDanHomeDetailHeaderViewCell.h"
#import "QuDanHomDetailBasicViewCell.h"
#import "QuDanHomeDetailServerViewCell.h"
#import "QuDanHomeDetailImageViewCell.h"
#import "QuDanHomeDetailAlterViewCell.h"
#import "QuDanHomeDetailCommentViewCell.h"
#import "QuDanHomeDetailEmptyViewCell.h"
#import "QuDanHomeDetailCommentHeaderViewCell.h"
#import "QuDanHomeConfirmOrderController.h"
#import "QDHomeNetWork.h"
#import "QuDanHomdeOrderDetailModel.h"
@interface QuDanHomeDetailController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UICollectionView *detailCollectionView;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
@property (nonatomic,strong)QuDanHomdeOrderDetailModel *orderDetailModel;

@end
@implementation QuDanHomeDetailController
- (UICollectionView *)detailCollectionView
{
    if (!_detailCollectionView) {
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
        flow.sectionHeadersPinToVisibleBounds = NO;//头视图悬浮
        flow.scrollDirection = UICollectionViewScrollDirectionVertical;
        flow.minimumLineSpacing = 0;//行间距
        flow.minimumInteritemSpacing = 0;//列间距
        _detailCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flow];
        _detailCollectionView.backgroundColor =  [UIColor clearColor];
        _detailCollectionView.showsVerticalScrollIndicator = NO;
        _detailCollectionView.showsHorizontalScrollIndicator = NO;
        _detailCollectionView.delegate = self;
        _detailCollectionView.dataSource = self;
        [_detailCollectionView registerNib:[UINib nibWithNibName:@"QuDanHomeDetailHeaderViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanHomeDetailHeaderViewCell"];
        [_detailCollectionView registerNib:[UINib nibWithNibName:@"QuDanHomDetailBasicViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanHomDetailBasicViewCell"];
        [_detailCollectionView registerNib:[UINib nibWithNibName:@"QuDanHomeDetailServerViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanHomeDetailServerViewCell"];
        [_detailCollectionView registerNib:[UINib nibWithNibName:@"QuDanHomeDetailServerViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanHomeDetailServerViewCell"];
        [_detailCollectionView registerNib:[UINib nibWithNibName:@"QuDanHomeDetailServerViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanHomeDetailServerViewCell"];
        [_detailCollectionView registerNib:[UINib nibWithNibName:@"QuDanHomeDetailImageViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanHomeDetailImageViewCell"];
        [_detailCollectionView registerNib:[UINib nibWithNibName:@"QuDanHomeDetailAlterViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanHomeDetailAlterViewCell"];
        [_detailCollectionView registerNib:[UINib nibWithNibName:@"QuDanHomeDetailCommentHeaderViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanHomeDetailCommentHeaderViewCell"];
        [_detailCollectionView registerNib:[UINib nibWithNibName:@"QuDanHomeDetailCommentViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanHomeDetailCommentViewCell"];
        [_detailCollectionView registerNib:[UINib nibWithNibName:@"QuDanHomeDetailEmptyViewCell" bundle:nil] forCellWithReuseIdentifier:@"QuDanHomeDetailEmptyViewCell"];
    }
    return _detailCollectionView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = QD_RGB(247, 248, 252);
    self.navigationItem.title = @"内容详情";
    self.nextBtn.layer.cornerRadius = 20.0f;
    self.nextBtn.layer.masksToBounds= YES;
    self.nextBtn.backgroundColor = [UIColor bm_colorGradientChangeWithSize:CGSizeMake(QD_IPHONE_WIDTH-60, 40) direction:PageGradientChangeDirectionLevel startColor:PostColorP(0xFF5121,1) endColor:PostColorP(0xFFC021,0.5)];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"qd_back"] forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, 0, 40 , 40);
    btn.imageEdgeInsets = UIEdgeInsetsMake(0.0, -15.0, 0.0, 15.0);
    [btn addTarget:self action:@selector(onLeftBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem = barItem;
    [self.view addSubview:self.detailCollectionView];
    [self.detailCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.bottom.offset(-80);
    }];
    [self setupOderDetailData];
}
-(void)setupOderDetailData
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"publishOrderId"] = self.publishOrderId;
    [[QDHomeNetWork shareInstance]requestHomeOderDetail:param viewController:self andBlock:^(id  _Nullable data, BOOL result) {
        NSLog(@"订单详情.data:%@",data);
        self.orderDetailModel = [QuDanHomdeOrderDetailModel mj_objectWithKeyValues:data];
        [self.detailCollectionView reloadData];
    }];
}
-(void)onLeftBtnAction:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -- QuDanHomeDetailController 代理方法
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 6;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 5) {
        return 1;
    }else{
        return 1;
    }
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        QuDanHomeDetailHeaderViewCell *headerCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanHomeDetailHeaderViewCell" forIndexPath:indexPath];
        [headerCell.iconImage sd_setImageWithURL:[NSURL URLWithString:self.orderDetailModel.imageUrl] placeholderImage:[UIImage imageNamed:@"qd_test"]];
        headerCell.nameLab.text = self.orderDetailModel.nickName;
        headerCell.addressLab.text = [NSString stringWithFormat:@"来自于%@",self.orderDetailModel.area];
        headerCell.typeLab.text = [NSString stringWithFormat:@"%@",self.orderDetailModel.category];
        headerCell.priceLab.text = [NSString stringWithFormat:@"%.0f",[self.orderDetailModel.price floatValue]];
        return headerCell;
    }else if(indexPath.section == 1){
        QuDanHomDetailBasicViewCell *bascicCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanHomDetailBasicViewCell" forIndexPath:indexPath];
        bascicCell.biaoqianLab.text = [NSString stringWithFormat:@"%@,年龄%@",self.orderDetailModel.gender,self.orderDetailModel.ageLimit];
        bascicCell.timeLab.text      = [NSString stringWithFormat:@"%@",[self getWeekStr]];
        bascicCell.contentLab.text = [NSString stringWithFormat:@"%@",self.orderDetailModel.otherDesc];
        return bascicCell;
    }else if(indexPath.section == 2){
        QuDanHomeDetailImageViewCell *imageCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanHomeDetailImageViewCell" forIndexPath:indexPath];
        if (self.orderDetailModel.imgs.count > 0) {
            if (self.orderDetailModel.imgs.count == 1) {
                imageCell.firstImage.hidden = NO;
                imageCell.secodImage.hidden = YES;
                imageCell.thirdImage.hidden = YES;
                [imageCell.firstImage sd_setImageWithURL:[NSURL URLWithString:self.orderDetailModel.imgs[0]] placeholderImage:[UIImage imageNamed:@"qd_test"]];
            }else if(self.orderDetailModel.imgs.count == 2){
                imageCell.firstImage.hidden = NO;
                imageCell.secodImage.hidden = NO;
                imageCell.thirdImage.hidden = YES;
                [imageCell.firstImage sd_setImageWithURL:[NSURL URLWithString:self.orderDetailModel.imgs[0]] placeholderImage:[UIImage imageNamed:@"qd_test"]];
                [imageCell.secodImage sd_setImageWithURL:[NSURL URLWithString:self.orderDetailModel.imgs[1]] placeholderImage:[UIImage imageNamed:@"qd_test"]];
            }else{
                imageCell.firstImage.hidden = NO;
                imageCell.secodImage.hidden = NO;
                imageCell.thirdImage.hidden = NO;
                [imageCell.firstImage sd_setImageWithURL:[NSURL URLWithString:self.orderDetailModel.imgs[0]] placeholderImage:[UIImage imageNamed:@"qd_test"]];
                [imageCell.secodImage sd_setImageWithURL:[NSURL URLWithString:self.orderDetailModel.imgs[1]] placeholderImage:[UIImage imageNamed:@"qd_test"]];
                [imageCell.thirdImage sd_setImageWithURL:[NSURL URLWithString:self.orderDetailModel.imgs[2]] placeholderImage:[UIImage imageNamed:@"qd_test"]];
            }
        }
        return imageCell;
    }else if(indexPath.section == 3){
        QuDanHomeDetailAlterViewCell *alterCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanHomeDetailAlterViewCell" forIndexPath:indexPath];
        return alterCell;
    }else if(indexPath.section == 4){
        QuDanHomeDetailCommentHeaderViewCell *commentHeaderCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanHomeDetailCommentHeaderViewCell" forIndexPath:indexPath];
        return commentHeaderCell;
    }else {
        QuDanHomeDetailCommentViewCell *commentCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanHomeDetailCommentViewCell" forIndexPath:indexPath];
        return commentCell;
    }
}
-(NSString *)getWeekStr{
    NSMutableString *weekStr = [NSMutableString string];
    for (QuDanHomdeOrderDetailTimeItemModel *item in self.orderDetailModel.time) {
        [weekStr appendString:item.tips];
    }
    return weekStr;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 0) {
        return UIEdgeInsetsMake(0, 0, 0, 5);
    }else  if (section == 1) {
        return UIEdgeInsetsMake(5, 0, 0, 0);
    }else if (section == 2) {
        if (self.orderDetailModel.imgs.count == 0) {
            return UIEdgeInsetsZero;
        }else{
            return UIEdgeInsetsMake(5, 0, 0, 0);
        }
    }else if (section == 3) {
        return UIEdgeInsetsMake(5, 0, 0, 0);
    }else{
        return UIEdgeInsetsZero;
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake(QD_IPHONE_WIDTH ,65);
    }else if(indexPath.section == 1){
        CGFloat height = [self QDGetHeightWithText:self.orderDetailModel.otherDesc width:QD_IPHONE_WIDTH-30 font:15];
        return CGSizeMake(QD_IPHONE_WIDTH ,height + 90);
    }else if(indexPath.section == 2){
        if (self.orderDetailModel.imgs.count == 0) {
            return CGSizeMake(QD_IPHONE_WIDTH ,CGFLOAT_MIN);
        }else{
            return CGSizeMake(QD_IPHONE_WIDTH ,(QD_IPHONE_WIDTH-20)/3+20);
        }
    }else if(indexPath.section == 3){
        return CGSizeMake(QD_IPHONE_WIDTH ,100);;
    }else if(indexPath.section == 4){
        return CGSizeMake(QD_IPHONE_WIDTH ,40);
    }else{
        return  CGSizeMake(QD_IPHONE_WIDTH ,120);;
    }
}
- (float)QDGetHeightWithText:(NSString *)text width:(float)width font: (float)font;
{
    CGRect rect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil];
    return rect.size.height;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
}
/// 下一步
/// @param sender 下一步
- (IBAction)actionNextBtn:(UIButton *)sender
{
    QuDanHomeConfirmOrderController *confirmOrderVc = [[QuDanHomeConfirmOrderController alloc]init];
    confirmOrderVc.hidesBottomBarWhenPushed = YES;
    confirmOrderVc.publishOrderId = self.orderDetailModel.publishOrderId;
    [self.navigationController pushViewController:confirmOrderVc animated:YES];
}
@end
